package cn.sh.yhk.controller.basic;

import cn.sh.yhk.configure.security.SecurityUserAuthor;
import cn.sh.yhk.model.basic.SUser;
import cn.sh.yhk.service.basic.RoleService;
import cn.sh.yhk.service.basic.UserService;
import cn.sh.yhk.vo.BasicVo;
import cn.sh.yhk.vo.Common;
import cn.sh.yhk.vo.PageVo;
import cn.sh.yhk.vo.SUserVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @Resource
    RoleService roleService;

    @Resource
    SecurityUserAuthor securityUserAuthor;

    @ApiOperation(value = "用户登陆", notes = "用户登陆")
    @PostMapping("/login")
    BasicVo login(String username, String password, HttpServletRequest request) {
        SUser sUser = new SUser();
        sUser.setUserName(username == null ? "" : username);
        sUser.setPassword(password == null ? "" : password);
        BasicVo vo = userService.login(sUser);
        // UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
        //Authentication authentication = securityUserAuthor.loadUserByUsername(authRequest); //调用loadUserByUsername
        if (vo.getSuccess() == true) {
            request.getSession().setAttribute(Common.SESSION_LOGIN_USER_INFO, (SUserVo) vo.getObject());
        }
        return vo;
    }

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @PostMapping("/getuser/{id}")
    @ResponseBody
    BasicVo getUser(@PathVariable("id") Long id) {
        return userService.getUser(id);
    }

    @GetMapping("/initEditPage")
    String initEditPage(Long id, Model model) throws Exception {
        if (id != null) {
            SUserVo userVo = (SUserVo) userService.getUser(id).getObject();
            model.addAttribute("userinfo_role", userVo);
            return "userinfo";
        } else {
            model.addAttribute("roles", roleService.getList().getObject());
            return "userinfo";
        }
    }

    @GetMapping("/initPage")
    String initUserPage() throws Exception {
        return "user";
    }

    @ApiOperation(value = "用户列表分页", notes = "用户列表分页")
    @GetMapping("/getPage")
    @ResponseBody
    PageVo getUserPage(Integer page, Integer limit, SUserVo userVo) throws Exception {
        Page pagee = PageHelper.startPage(page, limit);
        BasicVo vo = userService.getUserPage(userVo);
        PageInfo info = new PageInfo<>(pagee.getResult());
        return new PageVo(info.getTotal(), info.getList());
    }

    @ApiOperation(value = "创建用户", notes = "创建用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userVo", value = "用户详细实体user", required = true),
            @ApiImplicitParam(name = "groups", value = "组ID，逗号隔开", required = true, dataType = "String"),
            @ApiImplicitParam(name = "menus", value = "菜单ID，逗号隔开", required = true, dataType = "String")
    })
    @PostMapping("/create")
    BasicVo createUser(SUserVo userVo, String groups, String menus) {
        return userService.createUser(userVo, groups, menus);
    }

    @ApiOperation(value = "删除用户", notes = "删除用户")
    @PostMapping("/delete/{id}")
    BasicVo deleteUser(@PathVariable("id") Long id) {
        return userService.deleteUser(id);
    }

    @ApiOperation(value = "用户退出", notes = "用户退出")
    @PostMapping("/logout")
    public void logout(HttpServletRequest request) {
        request.getSession().invalidate();
    }


    @ApiOperation(value = "创建用户与用户组", notes = "创建用户与用户组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "groupId", value = "组ID", required = true, dataType = "String")
    })
    @PostMapping("/createusergroup")
    BasicVo createUserGroup(Long userId, Long groupId) {
        return userService.createUserGroup(userId, groupId);
    }

    public String HumpToUnderline(String para) {
        StringBuilder sb = new StringBuilder(para);
        int temp = 0;//定位
        for (int i = 0; i < para.length(); i++) {
            if (Character.isUpperCase(para.charAt(i))) {
                sb.insert(i + temp, "_");
                temp += 1;
            }
        }
        return sb.toString().toUpperCase();
    }
}
